Skip to content

Session is not set in object if connection fails, which raises 'AttributeError' on __del__ (#746)#877

Open
peco-engineer-bot[bot] wants to merge 2 commits into
mainfrom
ai/issue-746
Open

Session is not set in object if connection fails, which raises 'AttributeError' on __del__ (#746)#877
peco-engineer-bot[bot] wants to merge 2 commits into
mainfrom
ai/issue-746

Conversation

@peco-engineer-bot

Copy link
Copy Markdown
Contributor

Summary

Automated fix for #746 — Session is not set in object if connection fails, which raises 'AttributeError' on del.

Added a hasattr(self, "session") guard at the top of Connection.__del__ in src/databricks/sql/client.py so that when the Session constructor raises before self.session is assigned, garbage-collecting the half-constructed Connection no longer triggers AttributeError via the open property. Verified with the E2E test that forces a constructor-stage failure (username/password → ValueError) and asserts nothing escapes del.

Root cause & plan

Root cause: In Connection.init (src/databricks/sql/client.py:321), self.session = Session(...) runs inside a try/except. If the Session CONSTRUCTOR raises before the assignment completes (e.g. client-side auth validation such as passing username/password -> ValueError, or use_kernel+use_sea -> ValueError, or an OAuth provider whose constructor makes a failing network call), then self.session is never bound and the exception propagates out of init. When the half-constructed Connection is garbage-collected, del (client.py:464) executes if self.open:, and the open property (client.py:509) evaluates self.session.is_open. Since session is absent, this raises AttributeError: 'Connection' object has no attribute 'session', which Python reports as an 'Exception ignored in del' warning on stderr. Note: a bad host / bad token does NOT trigger this, because those fail in Session.open() (after self.session is assigned); only a constructor-stage failure leaves session unset.
Files: src/databricks/sql/client.py, tests/e2e/test_driver.py
Planned coverage:

  • Constructing a Connection whose Session construction fails before self.session is assigned must not raise AttributeError from del during garbage collection. Force a constructor-stage connection failure (against the live-warehouse connection params), let the failed Connection be collected, and assert that (a) the original connection error propagates and (b) no 'AttributeError: ... has no attribute session' / 'Exception ignored in del' is emitted. Will be red before the fix, green after. (del AttributeError when session attribute is never set due to a constructor-stage connection/initialization failure)

Files changed

  • tests/e2e/test_driver.py
  • src/databricks/sql/client.py

Test plan

  • tests/e2e/test_driver.py::TestPySQLConnectionFailureSuite::test_del_does_not_raise_when_session_never_set — fails (red) against the original code, passes (green) after the fix

🤖 Generated by engineer-bot (bug-fix flow) — review before merge.

…buteError' on __del__ (#746)

Signed-off-by: peco-engineer-bot[bot] <3815206+peco-engineer-bot[bot]@users.noreply.github.com>
@peco-engineer-bot peco-engineer-bot Bot added the engineer-bot Maintainer-applied gate: triggers engineer-bot (bug-fix on issue / take-over on PR). label Jul 23, 2026

@peco-review-bot peco-review-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low

Looks good — the hasattr(self, "session") guard in Connection.__del__ correctly fixes the AttributeError, since the username/password ValueError is raised inside Session.__init__ (session.py:121 → auth.py:109) before self.session is assigned. One low note: the regression test is E2E-only despite covering purely client-side behavior that could be unit-tested.

Comment thread tests/e2e/test_driver.py
Addresses:
  - #3635886434 at tests/e2e/test_driver.py:1159

Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>

@peco-review-bot peco-review-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No issues identified by the review bot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

engineer-bot Maintainer-applied gate: triggers engineer-bot (bug-fix on issue / take-over on PR).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants